package com.wf.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.wf.dto.Apart_houses;
import com.wf.dto.ContractMap;
import com.wf.dto.TaskDTO;
import com.wf.service.ApprovalService;
import com.wf.service.ContractInfoService;
import com.wf.service.MessageInfoService;
import com.wf.vo.Result;
import com.wf.vo.TaskRepresentationVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.history.HistoricActivityInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *  审批接口
 * @author mpk
 */
@RestController
@Slf4j
@DS("flowable")
@RequestMapping("/approval")
public class ApprovalController {


    @Autowired
    private ApprovalService approvalService;

    @Autowired
    MessageInfoService messageInfoService;
    @Autowired
    ContractInfoService contractInfoService;

    /**
     * 启动审批流程
     */
    @RequestMapping(value="/process", method= RequestMethod.POST)
    @DS("master")
    public Result startProcessInstance(@RequestBody Apart_houses apartHouses, @RequestParam(value = "phone",required = false) String phone, @RequestParam(value = "money",required = false) Integer money) {
        ContractMap map = contractInfoService.generateContractMap(apartHouses, phone, money);
        boolean b = approvalService.startProcess(map);
        return Result.success(b);
    }
    /**
     * 获取任务
     * @param assignee 处理人ID
     * @return
     */
    @RequestMapping(value="/tasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public Result getTasks(@RequestParam String assignee) {
        List<TaskRepresentationVO> taskRepresentations = approvalService.getTasks(assignee);
        return Result.success(taskRepresentations);
    }

    /**
     * 完成任务
     * @param taskDTO
     */
    @PostMapping("/complete")
    public Result completeTask(@RequestBody TaskDTO taskDTO)
    {
        System.out.println("taskDTO = " + taskDTO);
        log.info(String.valueOf(taskDTO));
        Result result = approvalService.completeTask(taskDTO);
        return Result.success(result);
    }


    /**
     * 获取历史记录
     * @param assignee 处理人ID
     * @return
     */
    @GetMapping("history")
    public Result getHistory(String assignee)
    {

        List<HistoricActivityInstance> history = approvalService.getHistory(assignee);

        return Result.success(history);

    }


}